package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author BAGUS
 */
public class Penerimaan {

    private String kodePenerimaan;
    private String waktuPenerimaan;
    private double jumlahGasElpiji;
    private String jenisGasElpiji;
    private double hargaGasElpiji;
    private String username;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    public double getHargaGasElpiji() {
        return hargaGasElpiji;
    }

    public String getJenisGasElpiji() {
        return jenisGasElpiji;
    }

    public double getJumlahGasElpiji() {
        return jumlahGasElpiji;
    }

    public String getWaktuPenerimaan() {
        ConvertTanggal convertTanggal = new ConvertTanggal();
        return convertTanggal.getTanggalPenuh();
    }

    public void setHargaGasElpiji(double hargaGasElpiji) {
        this.hargaGasElpiji = hargaGasElpiji;
    }

    public void setJenisGasElpiji(String jenisGasElpiji) {
        this.jenisGasElpiji = jenisGasElpiji;
    }

    public void setJumlahGasElpiji(double jumlahGasElpiji) {
        this.jumlahGasElpiji = jumlahGasElpiji;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void insertPenerimaan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("insert into penerimaan values ('"
                    + getKodePenerimaan() + "','"
                    + getWaktuPenerimaan() + "',"
                    + getJumlahGasElpiji() + ",'"
                    + getJenisGasElpiji() + "',"
                    + getHargaGasElpiji() + ",'"
                    + getUsername() + "')");
            GasElpiji gasElpiji = new GasElpiji();
            gasElpiji.setHarga(getHargaGasElpiji());
            gasElpiji.setJenis(getJenisGasElpiji());
            gasElpiji.setJumlah(getJumlahGasElpiji());
            gasElpiji.tambahGasElpiji();
            gasElpiji.updateHargaGasElpiji();
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }

    public String getKodePenerimaan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepenerimaan from penerimaan order by kodepenerimaan");
            String kode_penerimaan = "";
            ConvertTanggal convertTanggal = new ConvertTanggal();
            while (resultSet.next()) {
                kode_penerimaan = resultSet.getString("kodepenerimaan");
            }
            int no_penerimaan = 0;
            if ("".equals(kode_penerimaan)) {
                this.kodePenerimaan = convertTanggal.getTanggal() + "101";
            } else {
                String data = null;
                if(kode_penerimaan.length()==9){
                    data = kode_penerimaan.substring(0, 6);
                }else if(kode_penerimaan.length()==8){
                    data = kode_penerimaan.substring(0, 5);
                }
                if (convertTanggal.cekWaktu(data) == true) {
                    no_penerimaan = Integer.parseInt(kode_penerimaan) + 1;
                    this.kodePenerimaan = String.valueOf(no_penerimaan);
                } else {
                    this.kodePenerimaan = convertTanggal.getTanggal() + "101";
                }
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            this.kodePenerimaan = null;
        }
        return kodePenerimaan;
    }
}
